package 历年真题;

import java.util.*;

/**
 * Created by lenovo on 2017/3/29.
 */
public class q30 {
    /**
     * 计算差值
     * @param n
     * @return
     */
    private static int MaxSubMin(int n){
        StringBuffer sb = new StringBuffer(""+n);
        while(sb.length() < 5){
            sb.insert(0,'0');
        }
        char ch[] = (sb.toString()).toCharArray();
        Arrays.sort(ch);

        int max = (ch[4] - '0')*10000 + (ch[3] - '0')*1000 +(ch[2] - '0')*100 +(ch[1] - '0')*10 +(ch[0] - '0');
        int min = (ch[0] - '0')*10000 + (ch[1] - '0')*1000 +(ch[2] - '0')*100 +(ch[3] - '0')*10 +(ch[4] - '0');

        return max - min;
    }

    private static Set<Integer> blackHole(int n){
        Set<Integer> set  = new LinkedHashSet<Integer>();
        set.add(n);
        while(true){
            n = MaxSubMin(n);
            if(set.add(n) == false){
                Set<Integer> tSet = new LinkedHashSet<Integer>();
                for(Iterator iterator = set.iterator();iterator.hasNext();){
                    Integer integer = (Integer) iterator.next();
                    if(integer == n){
                        break;
                    }else{
                        tSet.add(integer);
                    }
                }
                set.removeAll(tSet);
                break;
            }
        }
        return set;
    }

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        Set<Set<Integer>> set  = new LinkedHashSet<>();
        for(int i=10000;i<100000;i++){
             set.add(blackHole(n));
        }

        for(Iterator iterator = set.iterator();iterator.hasNext();){
            Set<Integer> set2 = (Set<Integer>) iterator.next();
            System.out.println(set2);
        }
    }
}
